﻿using System;
using System.Linq;
using XRM.Security.DataAccess;
using XRM.Security.DataAccess.Enums;
using XRM.Security.DataAccess.Models;
using XRM.Security.WebUI.Consts;
using XRM.Security.WebUI.Models.UserRegister;

namespace XRM.Security.WebUI.Services
{
    public class HistoryService
    {
        public IDataManager DataManager { get; private set; }
        private readonly int _systemUserId;
        
        public HistoryService(IDataManager dataManager, int systemUserId)
        {
            DataManager = dataManager;
            _systemUserId = systemUserId;
        }

        #region PC

        public void UpdatePc(UserRegisterAddPCModel newPc, PC oldPc)
        {
            var userId = newPc.UserRegisterId;
            const UserHistoryGroup historyGroup = UserHistoryGroup.Pc;
            if ( newPc.SerialNumber != oldPc.SerialNumber )
                AddHistoryEvent( String.Format( HistoryTemplate.PCSerialNumberChange, oldPc.SerialNumber, newPc.SerialNumber ), userId, historyGroup );
            if ( newPc.PCHost != oldPc.PCHost )
                AddHistoryEvent( String.Format( HistoryTemplate.PCHostnameChange, oldPc.PCHost, newPc.PCHost ), userId, historyGroup );
            foreach ( var sticker in oldPc.Stickers )
            {
                if ( newPc.StickerList.All( s => s.StickerNumber != sticker.StickerNumber ) )
                {
                    AddHistoryEvent( String.Format( HistoryTemplate.PCStickerDel, sticker.StickerNumber, newPc.SerialNumber ), userId, historyGroup );
                }
            }
            foreach (var sticker in newPc.StickerList)
            {
                if (oldPc.Stickers.All(s => s.StickerNumber != sticker.StickerNumber))
                {
                    AddHistoryEvent( String.Format( HistoryTemplate.PCStickerAdd, sticker.StickerNumber, newPc.SerialNumber ), userId, historyGroup );
                }
            }
        }

        public void AddPc( UserRegisterAddPCModel newPc )
        {
            var userId = newPc.UserRegisterId;
            const UserHistoryGroup historyGroup = UserHistoryGroup.Pc;
            AddHistoryEvent(String.Format(HistoryTemplate.PCAdd, newPc.SerialNumber), userId, historyGroup);
            foreach (var sticker in newPc.StickerList)
            {
                AddHistoryEvent( String.Format( HistoryTemplate.PCStickerAdd, sticker.StickerNumber, newPc.SerialNumber ), userId, historyGroup );
            }
        }

        public void DeletePC( UserPC userPc )
        {
            AddHistoryEvent( string.Format(HistoryTemplate.PCDel, userPc.PC.SerialNumber), userPc.UserRegisterId, UserHistoryGroup.Pc );
        }

        #endregion

        private void AddHistoryEvent(string text, int userId, UserHistoryGroup historyGroup)
        {
            DataManager.UserHistoryRepository.Add( new UserHistory
                {
                    HistoryDate = DateTime.Now,
                    HistoryText = text,
                    HistoryGroup = (int)historyGroup,
                    SystemUserId = _systemUserId,
                    UserRegisterId = userId
                });
        }
    }
}